import {
  addUserList,
  getUserList,
  getUserInfoById,
  updateUserInfoById,
  deleteUserInfoById,
  getUserEnabled,
  getRoleList,
  getAllRoleList,
} from '@/network/api/userListApi';

let initState = () => ({
  // 列表信息
  tabList: [], //用户信息列表
  total: 0,
  pageSize: 10,
  pageNum: 1,
  searchValue: '', //用户搜索信息
  password: '', //用于修改的密码
  addVisible: false, //弹出框开关
  addVisibleModalType: 'add', //弹出框类型
  addVisibleModalUserId: '', //模态框userId
  allRoleList: [], //所有角色集合
});

export default {
  namespace: 'UserList',
  state: { ...initState() },
  effects: {
    //查询所有角色
    *getAllRoleList({ payload }, { call, put }) {
      return yield call(getAllRoleList);
    },
    // 获取角色列表
    *getRoleList({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(getRoleList, obj);
    },
    // 添加用户
    *addUserList({ payload }, { call, put }) {
      var obj = { body: payload };
      return yield call(addUserList, obj);
    },
    // 分页获取用户列表
    *getUserList({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(getUserList, obj);
    },
    // 获取用户详情
    *getUserInfoById({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(getUserInfoById, obj);
    },
    // 修改用户信息
    *updateUserInfoById({ payload }, { call, put }) {
      var obj = { body: payload };
      return yield call(updateUserInfoById, obj);
    },
    // 删除用户
    *deleteUserInfoById({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(deleteUserInfoById, obj);
    },
    // 启用/禁用用户
    *getUserEnabled({ payload }, { call, put }) {
      var obj = { body: payload };
      return yield call(getUserEnabled, obj);
    },
  },
  reducers: {
    setState(state, action) {
      return { ...state, ...action.payload };
    },
    init(state, action) {
      return { ...initState(), ...action.payload };
    },
  },
};
